﻿#region

using System;
using System.Reflection;
using DomainModel.Domain.Model;
using DomainModel.Domain.Model.MessageAssemblyModel;
using DomainModel.Domain.Model.MessageTypeModel;
using DomainModel.Domain.Model.ServiceModel;
using DomainModel.Utils;
using EsbMonitor.Ninject;
using Iesi.Collections.Generic;
using log4net.Config;
using MessageTypeDescriber.Xml;
using Ninject;
using TestHost;

#endregion

namespace DbTest
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            BasicConfigurator.Configure();

            var initializer = new Initializer();
            initializer.Run();

            var servicesRepo = KernelContainer.Kernel.Get<IServicesRepository>();
            var messageTypesRepo = KernelContainer.Kernel.Get<IMessageTypesRepository>();
            var messageAssembliesRepo = KernelContainer.Kernel.Get<IMessageAssembliesRepository>();

            Service service = new Service(servicesRepo.NextServiceId());
            service.CreateModifyInfo = new CreateModifyInfo
                                           {
                                               Created = DateTime.Now,
                                               LastModified = DateTime.Now
                                           };
            service.FirstSeen = DateTime.Now;
            service.InputQueue = new QueueInfo("FakeInputQueue");
            service.LastAlive = DateTime.Now;
            service.LastStarted = DateTime.Now;
            service.Name = "My Fake Service";
            service.Summary = "This is only a fake service";
            service.Version = "1.0";

            servicesRepo.Store(service);

            service = servicesRepo.Get(new QueueInfo("FakeInputQueue"));

            Handle(service);


            Console.WriteLine(" --- OK ---");
            Console.ReadLine();
        }



        public static void Handle(Service service)
        {
            var servicesRepository = KernelContainer.Get<IServicesRepository>();


            var messageAssembliesRepository = KernelContainer.Get<IMessageAssembliesRepository>();
            var md5Hasher = KernelContainer.Get<IMd5Hasher>();


            var messageAssembly = new MessageAssembly(messageAssembliesRepository.NextMessageAssemblyId())
                                      {
                                          Name = "Fake Assembly Name",
                                          FileName = "fakeName.dll",
                                          FileSize = 13454,
                                          LocalName = "d:asdasdas",
                                          Hash = "hash"
                                      };

            var messageTypes = StoreTypesFromMessageAssembly();

            foreach (var m in messageTypes)
            {
                Console.WriteLine("messageType.id: " + m.Id);
            }

            foreach (var messaageType in messageTypes)
            {
                messageAssembly.AddMessageType(messaageType);
            }

            messageAssembliesRepository.Store(messageAssembly);


            service.AddMessageAssembly(messageAssembly);

            servicesRepository.Store(service);
        }

        private static ISet<MessageType> StoreTypesFromMessageAssembly()
        {
            Assembly assembly = typeof (TestHndl).Assembly;

            ISet<MessageType> messageTypesSet = new HashedSet<MessageType>();

            foreach (var type in assembly.GetTypes())
            {
                MessageType messageType = StoreType(type);
                messageTypesSet.Add(messageType);
            }

            return messageTypesSet;
        }

        private static MessageType StoreType(Type type)
        {
            var messageTypesRepository = KernelContainer.Get<IMessageTypesRepository>();
            var md5Hasher = KernelContainer.Get<IMd5Hasher>();

            var typeDescription = new TypeFullDescription(type);
            string xml = TypeFullDescription.ToXml(typeDescription);
            string hash = md5Hasher.Md5(xml);

            MessageType messageType = messageTypesRepository.Get(hash);

            if (messageType == null)
            {
                messageType = new MessageType(messageTypesRepository.NextMessageTypeId())
                                  {
                                      Hash = hash,
                                      Name = type.FullName,
                                      TypeDescription = {TypeFullDescription = typeDescription}
                                  };

                messageTypesRepository.Store(messageType);
            }

            return messageType;
        }
    }
}